package org.hackystat.devcathlon.ui.page.profile;

import static org.junit.Assert.*;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.devcathlon.configuration.ConfigurationException;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.hackystat.devcathlon.ui.DevcathlonApplication;
import org.hackystat.devcathlon.ui.DevcathlonSession;
import org.hackystat.devcathlon.ui.page.teams.profile.TeamsProfile;
import org.junit.Before;
import org.junit.Test;

/**
 * Test Profile Panel class.
 * @author Anthony Du
 *
 */
public class TestProfilePanel extends DevcathlonTestHelper {
  private WicketTester tester;
  /**
   * Setup Profile session.
   * @throws Exception when an error occurs
   */
  @Before
  public void setUp() throws Exception {
    tester = new WicketTester(new DevcathlonApplication());
    tester.startPage(ProfilePage.class);
    // create team & project
    String teamName = "TestTeam";
    String owner = "administrator";
    String[] members = {"member1", "member2"};
    makeTeam(teamName, owner, members);
    // login
    FormTester formTester = tester.newFormTester("loginForm");
    formTester.setValue("user", "administrator@hackystat.org");
    formTester.setValue("password", "administrator@hackystat.org");
    formTester.submit();
    // render Profile main page
    tester.assertRenderedPage(ProfilePage.class);
  }
  
  /**
   * Test getting list of profile teams.
   * @throws Exception when an error occurs 
   */
  @Test
  public void testTeamProfileLinks() throws Exception {
    ProfileSession session = DevcathlonSession.get().getProfileSession();
    //test have team(s)
    assertNotNull("teams not null", session.getTeams());
    assertFalse("teams list should contain team(s)", session.getTeams().isEmpty());
    tester.assertLabel("profilePanel:teamsPanel:boxContent:teams:0:teamProfileLink:teamName", 
                        "TestTeam");
    tester.clickLink("profilePanel:teamsPanel:boxContent:teams:0:teamProfileLink");
    tester.assertRenderedPage(TeamsProfile.class);
  }
  
  /**
   * Test avatar.
   * @throws ConfigurationException when an error occurs
   */
  @Test
  public void testAvatar() throws ConfigurationException {
    tester.assertRenderedPage(ProfilePage.class);
    assertNotNull("test avatar exists", tester.getTagByWicketId("avatar"));
  }
}
